package com.sir.tao.carhire.controller;

import com.sir.tao.carhire.common.CommonResult;
import com.sir.tao.carhire.entity.vo.HeaderVo;
import com.sir.tao.carhire.entity.vo.MapVo;
import com.sir.tao.carhire.entity.vo.Vo;
import com.sir.tao.carhire.service.ManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author tao
 * @data 2021/12/6 10:48
 * @description: 管理员登录接口
 */
@RestController
@Api(tags = "经营管理")
@RequestMapping("/management")
public class ManagementController {
    @Resource
    private ManagementService managementService;

    /**
     * 获取头部
     * @return 获取头部
     */
    @ApiOperation("获取头部")
    @ApiImplicitParam(name = "获取头部", value = "获取头部")
    @PostMapping("/header")
    public CommonResult<?> getHeader(@RequestBody Vo vo){
        boolean b = checkExist(vo);
        if(b){
            return CommonResult.fail("开始时间和结束时间不能为空");
        }
        HeaderVo headerVo =  managementService.getHeader(vo);

        return CommonResult.success(headerVo);
    }

    /**
     * 获取body
     * @return 获取body
     */
    @ApiOperation("获取body")
    @ApiImplicitParam(name = "获取body", value = "获取body")
    @PostMapping("/body")
    public CommonResult<?> getBody(@RequestBody Vo vo){
        boolean b = checkExist(vo);
        if(b){
            return CommonResult.fail("开始时间和结束时间不能为空");
        }

        Map<String, TreeMap<String, BigDecimal>> body  = managementService.getBody(vo);
        List<MapVo> mapVoList = new ArrayList<>();
        for (Map.Entry<String, TreeMap<String, BigDecimal>> stringTreeMapEntry : body.entrySet()) {
            MapVo mapVo = new MapVo();
            mapVo.setName(stringTreeMapEntry.getKey());
            TreeMap<String, BigDecimal> value = stringTreeMapEntry.getValue();

            List<String> dates = new ArrayList<>();
            List<BigDecimal> numberS = new ArrayList<>();
            for (Map.Entry<String, BigDecimal> stringBigDecimalEntry : value.entrySet()) {
                dates.add(stringBigDecimalEntry.getKey());
                numberS.add(stringBigDecimalEntry.getValue());
            }
            mapVo.setDates(dates);
            mapVo.setNumberS(numberS);

            mapVoList.add(mapVo);

        }

        return CommonResult.success(mapVoList);
    }
    private boolean checkExist(Vo managementVo) {
        return Objects.isNull(managementVo) || Objects.isNull(managementVo.getStartTime()) || Objects.isNull(managementVo.getEndTime());
    }


}
